package com.syni.mdd.yhd.common.all.service.impl;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.component.utils.common.utils.BeanUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.controller.pay.aliPay.AliPayController;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsBusinessInfoDao;
import com.syni.mdd.yhd.common.all.dto.requestDto.pay.PayReDto;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessInfo;
import com.syni.mdd.yhd.common.all.entity.BmsDxCoupon;
import com.syni.mdd.yhd.common.all.entity.BmsDxNews;
import com.syni.mdd.yhd.common.all.service.*;
import com.syni.mdd.yhd.common.all.utils.*;
import com.syni.mdd.yhd.common.all.vo.rq.businessInfo.AddBusinessAlipayInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @className
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsBusinessInfoServiceImpl extends BaseService<BmsBusinessInfo, Integer> implements BmsBusinessinfoService {

    @Autowired
    BmsBusinessInfoDao bmsBusinessInfoDao;

    @Autowired
    BmsUserBusinessService bmsUserBusinessService;

    @Autowired
    BmsDxNewsService bmsDxNewsService;

    @Autowired
    AliPayController aliPayController;

    @Autowired
    BmsPazarConditionService bmsPazarConditionService;

    @Override
    public BmsBusinessInfoDao getBaseDao() {
        return bmsBusinessInfoDao;
    }

    @Override
    public boolean checkBusinessHavingPayInfo(Integer businessId) {
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("businessId",businessId);
        conditionMap.put("alipayStatus", Constants.COMMON_FLAG_TRUE_INT);
        conditionMap.put("alipayUid_NN",Constants.COMMON_FLAG_TRUE_INT);
        List<BmsBusinessInfo> bmsBusinessInfoList = bmsBusinessInfoDao.findAll(new SpecificationUtils(conditionMap));
        if(bmsBusinessInfoList != null && bmsBusinessInfoList.size() > 0){
            return true;
        }
        return false;
    }

    @Override
    public Map<String, Object> payTreasureConfirm(ParamsMap paramsMap) {
        final String repeatConfirmCode = "01";
        Integer businessInfoId = paramsMap.getInteger("businessInfoId");
        Integer businessId = paramsMap.getInteger("businessId");
        Integer userId = paramsMap.getInteger("userIdentify");
        Integer newId = paramsMap.getInteger("newId");
        Integer status = paramsMap.getInteger("status");
        if(status == null){
            status = Constants.COMMON_FLAG_TRUE_INT;
        }
        Optional<BmsBusinessInfo> bmsBusinessInfoOptional = bmsBusinessInfoDao.findById(businessInfoId);
        if(!bmsBusinessInfoOptional.isPresent()){
            return ResultMapHelper.failure(ResultMsg.CODE_BUSINESSINFO_NO_EXIST);
        }
        Map<String,Object> checkMap = bmsUserBusinessService.checkBusinessIsHaving(userId,businessId);
        if(!ResultCode.CODE_NORMAL.equals(checkMap.get("status").toString())){
            return checkMap;
        }
        BmsBusinessInfo bmsBusinessInfo = bmsBusinessInfoOptional.get();
        if(bmsBusinessInfo.getAlipayStatus() == Constants.COMMON_FLAG_TRUE_INT){
            return ResultMapHelper.result(repeatConfirmCode,ResultMsg.CODE_BUSINESSINFO_IS_CONFIRM);
        }
        if(status == Constants.COMMON_FLAG_TRUE_INT) {
            bmsBusinessInfo.setAlipayStatus(Constants.COMMON_FLAG_TRUE_INT);
            bmsBusinessInfo.setAlipayCorrect(Constants.COMMON_FLAG_TRUE_INT);
            //修改消息状态
        }else {
            bmsBusinessInfo.setAlipayCorrect(Constants.COMMON_FLAG_FALSE_INT);
        }
        bmsBusinessInfoDao.save(bmsBusinessInfo);
        Optional<BmsDxNews> bmsDxNewsOptional = bmsDxNewsService.getBaseDao().findById(newId);
        if(bmsDxNewsOptional.isPresent()){
            BmsDxNews bmsDxNews = bmsDxNewsOptional.get();
            String content = bmsDxNews.getContent();
            Map<String,Object> jsonMap = (Map<String, Object>) JSON.parse(content);
            jsonMap.put("status",Constants.COMMON_FLAG_TRUE_INT);
            bmsDxNews.setContent(JSON.toJSONString(jsonMap));
            bmsDxNewsService.getBaseDao().save(bmsDxNews);
        }
        if(bmsBusinessInfo.getAlipayStatus() == Constants.COMMON_FLAG_TRUE_INT){
            bmsPazarConditionService.updatePazarConditionPayData(businessId);
        }
        return ResultMapHelper.success(null,ResultMsg.HAND_SUCCESS);
    }

    @Override
    public Map<String, Object> createAlipay(ParamsMap paramsMap) {
        Integer businessId = paramsMap.getInteger("businessId");
        String alipayUid = paramsMap.getString("alipayUid").replace(" ","");
        String alipayUser = paramsMap.getString("alipayUser");
        Map<String,Object> paramsFen = new HashMap<>();
        paramsFen.put("businessPayId",alipayUid);
        paramsFen.put("payName",alipayUser);
        PayReDto payReDto = new PayReDto();
        payReDto.setPayName(alipayUser);
        payReDto.setBusinessPayId(alipayUid);
        String status = aliPayController.bind(payReDto).get("status").toString();
        if(!StringUtils.equals(status,"00")){
            return ResultMapHelper.failure("分账绑定失败");
        }
        List<BmsBusinessInfo> bmsBusinessInfoList = bmsBusinessInfoDao.findByBusinessId(businessId);
        BmsBusinessInfo bmsBusinessInfo;
        if(bmsBusinessInfoList.size() > 0){
            bmsBusinessInfo = bmsBusinessInfoList.get(0);
            bmsBusinessInfo.setAlipayStatus(1);
            bmsBusinessInfo.setAlipayCorrect(1);
            bmsBusinessInfo.setAlipayUser(alipayUser);
            bmsBusinessInfo.setAlipayUid(alipayUid);
        }else{
            bmsBusinessInfo = new BmsBusinessInfo();
            BeanUtils.populate(bmsBusinessInfo,paramsMap);
            bmsBusinessInfo.setAlipayNewTime(new Date());
            bmsBusinessInfo.setAlipayCorrect(1);
            bmsBusinessInfo.setAlipayStatus(1);
        }
        bmsBusinessInfoDao.save(bmsBusinessInfo);
        //统计收款信息
        bmsPazarConditionService.updatePazarConditionPayData(bmsBusinessInfo.getBusinessId());
        return ResultMapHelper.success("OK",ResultMsg.HAND_SUCCESS);
    }

    public boolean improveCollectionInfo(AddBusinessAlipayInfoVo addBusinessAlipayInfoVo) {
        Integer businessId = addBusinessAlipayInfoVo.getBusinessId();
        String alipayUid = addBusinessAlipayInfoVo.getAlipayUid().replace(" ","");
        String alipayUser = addBusinessAlipayInfoVo.getAlipayUser();
        PayReDto payReDto = new PayReDto();
        payReDto.setPayName(alipayUser);
        payReDto.setBusinessPayId(alipayUid);
        String status = aliPayController.bind(payReDto).get("status").toString();
        if(!StringUtils.equals(status,ResultCode.CODE_NORMAL)){
            return false;
        }
        List<BmsBusinessInfo> bmsBusinessInfoList = bmsBusinessInfoDao.findByBusinessId(businessId);
        BmsBusinessInfo bmsBusinessInfo;
        if(bmsBusinessInfoList.size() > 0){
            bmsBusinessInfo = bmsBusinessInfoList.get(0);
        }else{
            bmsBusinessInfo = new BmsBusinessInfo();
            bmsBusinessInfo.setAlipayNewTime(new Date());
        }
        bmsBusinessInfo.setBusinessId(businessId);
        bmsBusinessInfo.setAlipayStatus(1);
        bmsBusinessInfo.setAlipayCorrect(1);
        bmsBusinessInfo.setAlipayUser(alipayUser);
        bmsBusinessInfo.setAlipayUid(alipayUid);
        bmsBusinessInfo.setAlipayAccount(addBusinessAlipayInfoVo.getAlipayAccount());
        bmsBusinessInfo.setAlipayType(addBusinessAlipayInfoVo.getAlipayType());
        bmsBusinessInfoDao.save(bmsBusinessInfo);
        //统计收款信息
        bmsPazarConditionService.updatePazarConditionPayData(bmsBusinessInfo.getBusinessId());
        return true;
    }

    @Override
    public Map<Integer, BmsBusinessInfo> getBmsBusinessInfoMap(Set businessIds) {
        Map<Integer, BmsBusinessInfo> bmsBusinessInfoMap = new HashMap<>();
        List<BmsBusinessInfo> bmsBusinessInfos = bmsBusinessInfoDao.findByBusinessIdInAndAlipayStatus(businessIds,Constants.COMMON_FLAG_TRUE_INT);
        if(bmsBusinessInfos != null && bmsBusinessInfos.size() > 0){
            for(BmsBusinessInfo bmsBusinessInfo : bmsBusinessInfos){
                bmsBusinessInfoMap.put(bmsBusinessInfo.getBusinessId(),bmsBusinessInfo);
            }
        }
        return bmsBusinessInfoMap;
    }

    @Override
    public BmsBusinessInfo getBusinessInfoByBusinessId(Integer businessId) {
        List<BmsBusinessInfo> bmsBusinessInfos = bmsBusinessInfoDao.findByBusinessIdAndAlipayStatus(businessId, Constants.COMMON_FLAG_TRUE_INT);
        if (bmsBusinessInfos != null && bmsBusinessInfos.size() > 0) {
            return bmsBusinessInfos.get(0);
        }
        return null;
    }

    @Override
    public boolean checkAliPayUid(String alipayUser, String alipayUid) {
        PayReDto payReDto = new PayReDto();
        payReDto.setBusinessPayId(alipayUid);
        payReDto.setPayName(alipayUser);
        String status = aliPayController.bind(payReDto).get("status").toString();
        if(StringUtils.equals(status,"00")){
            return true;
        }
        return false;
    }

    @Override
    public Map<String, Object> getAlipayInfo(ParamsMap paramsMap) {
        return null;
    }
}
